﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;

namespace WowGuildSite.Armory
{
    internal static class ArmoryXmlUtility
    {
        internal static IList<XmlElement> GetChildElements( XmlElement element,string name )
        {
            List<XmlElement> result = new List<XmlElement>();

            if ( element == null )
            {
                return result;
            }

            foreach( XmlNode n in element.ChildNodes )
            {
                if ( n.Name == name && n is XmlElement )
                {
                    result.Add((XmlElement)n);
                }
            }
            return result;
        }

        internal static XmlElement  GetChildElement( XmlElement element,string name )
        {
            if ( element == null )
            {
                return null;
            }

            foreach (XmlNode n in element.ChildNodes)
            {
                if (n.Name == name && n is XmlElement)
                {
                    return (XmlElement)n;
                }
            }
            return null;
        }

        internal static string      GetChildElementText( XmlElement element,string name )
        {
            return GetElementText(GetChildElement(element, name));
        }

        internal static int         GetChildElementInt(XmlElement element,string name)
        {
            return ConvertInt(GetChildElementText(element, name));
        }

        internal static decimal     GetChildElementDecimal(XmlElement element,string name )
        {
            return ConvertDecimal(GetChildElementText(element, name));
        }

        internal static string      GetElementText( XmlElement element )
        {
            if ( element == null )
            {
                return string.Empty;
            }

            foreach( XmlNode n in element.ChildNodes )
            {
                if ( n is XmlText )
                {
                    return n.Value;
                }
            }

            return string.Empty;
        }

        internal static int         GetElementInt( XmlElement element )
        {
            return ConvertInt(GetElementText(element));
        }

        internal static decimal     GetElementDecimal(XmlElement element )
        {
            return ConvertDecimal(GetElementText(element));
        }

        internal static string      GetAttributeText( XmlAttribute attribute )
        {
            if ( attribute == null )
            {
                return string.Empty;
            }
            return attribute.Value;
        }

        internal static int         GetAttributeInt( XmlAttribute attribute )
        {
            return ConvertInt(GetAttributeText(attribute));
        }

        internal static decimal     GetAttributeDecimal( XmlAttribute attribute )
        {
            return ConvertDecimal(GetAttributeText(attribute));
        }

        internal static string      GetElementAttributeText( XmlElement element,string name )
        {
            if ( element == null )
            {
                return string.Empty;
            }

            return element.GetAttribute(name);
        }

        internal static int         GetElementAttributeInt( XmlElement element,string name )
        {
            return ConvertInt(GetElementAttributeText(element, name));
        }

        internal static decimal     GetElementAttributeDecimal( XmlElement element,string name )
        {
            return ConvertDecimal(GetElementAttributeText(element, name));
        }

        private static  int         ConvertInt( string text )
        {
            int result = 0;
            if (int.TryParse( text, NumberStyles.Any, CultureInfo.InvariantCulture, out result ))
            {
                return result;
            }
            return 0;
        }

        private static decimal  ConvertDecimal( string text )
        {
            decimal result = 0;
            if (decimal.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
            {
                return result;
            }
            return new decimal(0);
        }
    }
}
